﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace DAL
{
    public class D_Login
    {
        public D_Login()
        {}
        #region
        public bool IsExist(string strName, string strPass,out M_Login loginmodel)
        {
            M_Login model = new M_Login();
            string str = "P_SelectLogin";
            SqlParameter[] sqlparameters ={
            new SqlParameter("@login",SqlDbType.VarChar,50) ,
            new SqlParameter("@pass",SqlDbType.VarChar,50)                 
                                          };
            sqlparameters[0].Value = strName;
            sqlparameters[1].Value = strPass;
            SqlDataReader dtreader = DbHelper.getReader(str, sqlparameters);
            if (dtreader.Read())
            {
                model.User = dtreader["用户名"].ToString();
                model.UserName = dtreader["姓名"].ToString();
                model.Userno = dtreader["工号"].ToString();
                model.Password = dtreader["密码"].ToString();
                model.Department = dtreader["部门编号"].ToString();
                model.Company = dtreader["公司编号"].ToString();
                model.Email = dtreader["EMail"].ToString();
                model.Gender = dtreader["性别"].ToString();
                model.Call = dtreader["办公室电话"].ToString();
                model.Telphone = dtreader["手机号"].ToString();
                model.Postion = dtreader["岗位"].ToString();
                loginmodel = model;
                return true;
            }
            dtreader.Close();
            loginmodel = null;
            return false;
        }

        public string GetLoginNameById(string strId)
        {
            string strName = "";
            string str = "P_SelectNameById";
            SqlParameter[] sqlparameters ={
            new SqlParameter("@STR",SqlDbType.VarChar,50)                  
                                          };
            sqlparameters[0].Value = strId;
            SqlDataReader dtreader = DbHelper.getReader(str, sqlparameters);
            if (dtreader.Read())
            {
               strName = dtreader["姓名"].ToString();
            }
            return strName;
        }
        
        public int UpdatePassword(string strName,string strPassword)
        {
            string str = "P_UpdatePassword";
            SqlParameter[] sqlparameters ={
            new SqlParameter("@username",SqlDbType.VarChar,50),    
            new SqlParameter("@password",SqlDbType.VarChar,50)    
                                          };
            sqlparameters[0].Value = strName;
            sqlparameters[0].Value = strPassword;
            
            if (DbHelper.ExecuteCommand(str, sqlparameters) > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }            
        }

        #endregion
    }
}
